package com.lucy.nepu.system.period;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.nepu.loader.PeriodLoader;
import com.lucy.nepu.system.period.params.PeriodCreateParams;
import com.lucy.nepu.system.period.params.PeriodQueryParams;
import com.lucy.nepu.system.period.params.PeriodUpdateParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("nepu")
@Api(tags = "周期管理")
public class PeriodController {
    @Autowired
    PeriodService service;

    @Autowired(required = false)
    PeriodLoader periodLoader;

    //@TODO 当 父周期ID 为空时，需要校验考核周期时间段是否存在冲突
    @PostMapping("periods")
    @ApiOperation(value = "周期新建",notes = "")
    public ResponseDTO<Object> periodCreate(@RequestBody @Validated PeriodCreateParams params){
        Optional<Period> periodOp = service.getOne(params.getId());
        if (periodOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.BAD_REQUEST,"存在重复ID周期");
        }
        Period period = new Period();
        BeanUtil.copyProperties(params,period);
        return ResponseDTO.succData(
                service.saveOne(period)
        );
    }

    @DeleteMapping("periods/{id}")
    @ApiOperation(value = "周期删除",notes = "")
    public ResponseDTO<Object> periodDelete(@PathVariable("id") String id){
        service.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("periods")
    @ApiOperation(value = "周期更新",notes = "")
    public ResponseDTO<Object> periodUpdate(@RequestBody @Validated PeriodUpdateParams params){
        Optional<Period> periodOp = service.getOne(params.getId());
        if (!periodOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        Period period = periodOp.get();
        BeanUtil.copyProperties(params,period);
        return ResponseDTO.succData(
                service.updateOne(period)
        );
    }

    @GetMapping("periods")
    @ApiOperation(value = "周期列表",notes = "")
    public ResponseDTO<Object> periodListQuery(PeriodQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);

        Period period = new Period();
        BeanUtil.copyProperties(params,period, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<Period> example = Example.of(period);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("periods/{id}")
    @ApiOperation(value = "周期查询",notes = "")
    public ResponseDTO<Object> periodQuery(@PathVariable("id") String id){
        return ResponseDTO.succData(
                service.getOneUnwrap(id)
        );
    }

    @GetMapping("periods/now")
    @ApiOperation(value = "当前周期")
    public ResponseDTO<Object> periodNow(){
        String periodId = periodLoader.getPeriodId();
        return ResponseDTO.succData(
                service.getOneUnwrap(periodId)
        );
    }


}
